package moltencore.network.protocol;

import spark.network.Util;
import spark.network.protocol.Base;

public class TimeLeft extends Base {
	public static final short TYPE = 0x62;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private float secondsLeft;
	private static final int SECONDS_LEFT_OFFSET = Base.SIZE;
	private static final int SECONDS_LEFT_LENGTH = Float.SIZE / 8;

	private static final int SIZE =
			Base.SIZE +
			SECONDS_LEFT_LENGTH;

	public TimeLeft() {
		super(false, TYPE_BYTES);
	}

	public TimeLeft(float secondsLeft) {
		super(false, TYPE_BYTES);
		setSecondsLeft(secondsLeft);
	}

	public float getSecondsLeft() {
		return secondsLeft;
	}

	public void setSecondsLeft(float secondsLeft) {
		this.secondsLeft = secondsLeft;
	}

	@Override
	public int getSize() {
		return SIZE;
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		secondsLeft = Util.makeFloat(buffer, SECONDS_LEFT_OFFSET);
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		Util.arraycopy(Util.makeBytes(secondsLeft), 0, buffer, SECONDS_LEFT_OFFSET);
	}

	public static TimeLeft create(byte[] buffer, long receiveTime) {
		TimeLeft packet = new TimeLeft();
		packet.fill(buffer);
		packet.setTimeStampReceive(receiveTime);
		return packet;
	}
}